package com.pan.crawler.impl;

import com.pan.crawler.Search;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;

import java.time.Duration;
import java.util.Scanner;

public class SearchImpl implements Search {

    private final String searchUrl = "https://www.31xiaoshuo.com/search.php";

    private EdgeDriver edgeDriver;

    public SearchImpl(){
        edgeDriver = new EdgeDriver();
        try {
            edgeDriver.navigate().to(searchUrl);
            edgeDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
            WebElement search = edgeDriver.findElement(By.className("search"));
        }catch (Exception e){
            edgeDriver.quit();
            throw new RuntimeException("www.31xiaoshuo.com: 网站挂了");
        }
    }

    @Override
    public void find(String content) {
        WebElement search = edgeDriver.findElement(By.className("search"));
        search.sendKeys(content);
        search.submit();
    }

    @Override
    public void close() {
        edgeDriver.quit();
    }

    public String tips() {
        return "1.网站有点卡\n" +
                "2.下载还行吧";
    }

    public static void main(String[] args) {
        SearchImpl search;
        try {
            search = new SearchImpl();
        }catch (Exception e){
            System.out.println("错误");
            return ;
        }
        System.out.println("成功");
    }
}
